<template>
	<z-paging ref="paging" :refresherEnabled="false" :loadingMoreEnabled="false"
		:auto="false" :hideEmptyView="true">
		<view slot="top">
			<z-nav-bar backState="1000" type="transparentFixed">评论</z-nav-bar>
		</view>
		
		<view class="header">
			<view class="bg-white b-r-30 u-p-t-20 u-p-l-20" style="position: absolute; top: 180rpx;width: 94%;left: 20rpx;">
				<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1"
					:maxCount="4" width="77" height="77"></u-upload>
			</view>
		</view>
		
		<view class="u-p-30 bg-white b-r-50" style="margin-top: -40rpx;">
			<view>
				<u-textarea v-model="dataForm.userFirstComment.content" border="none" placeholder="展开说说你对商品的看法吧..." height="260"></u-textarea>
			</view>
			<view>
				<view class="u-flex u-m-t-30">
					<view class="u-flex-3 text-center">
						<text class="text-bold">商品评分</text>
					</view>
					<view class="u-flex-9">
						<u-rate count="5" v-model="dataForm.goodsScore" activeColor="#FFB143"></u-rate>
					</view>
				</view>
				
				<view class="u-flex u-m-t-30">
					<view class="u-flex-3 text-center">
						<text class="text-bold">商品评分</text>
					</view>
					<view class="u-flex-9">
						<u-rate count="5" v-model="dataForm.serverScore" activeColor="#FFB143"></u-rate>
					</view>
				</view>
				
				<view class="u-flex u-m-t-30">
					<view class="u-flex-3 text-center">
						<text class="text-bold">商品评分</text>
					</view>
					<view class="u-flex-9">
						<u-rate count="5" v-model="dataForm.deliveryScore" activeColor="#FFB143"></u-rate>
					</view>
				</view>
				
			</view>
		</view>
		
		<view slot="bottom">
			<view class="u-p-30 u-flex bg-white">
				<view class="u-flex-2">
					
				</view>
				<view class="u-flex-1">
					<u-button text="发布" shape="circle" :customStyle="{'background':'linear-gradient(270deg, #FF9D34 0%, #FFB455 45%, #FFCF7C 100%)', 'color': '#FFFFFF'}" @click="handleSubmit"></u-button>
				</view>
			</view>
		</view>
	</z-paging>
</template>

<script>
	import {addOrderComment} from '@/api/order_comment.js'
	import {uploadImage} from '@/api/upload.js'
	
	
	export default{
		data(){
			return{
				fileList1: [],
				dataForm:{
					userFirstComment:{
						content:''
					},
					goodsScore:5,
					serverScore:5,
					deliveryScore:5
				},
			}
		},
		onLoad(e) {
			let data = JSON.parse(decodeURIComponent(e.params))
			this.dataForm = {...this.dataForm, ...data}
			console.log(this.dataForm)
		},
		methods:{
			
			
			
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				this.hasClickSubmit = true
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result.remotePath
					}))
					fileListLen++
				}
				this.hasClickSubmit = false
			},
			
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			
			/**
			 * 商品评论图片
			 */
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					uploadImage(url).then(res => {
						resolve({
							localPath: url,
							remotePath: res
						})
					})
				})
			},
			
			/**
			 * 提交评论
			 */
			handleSubmit(){
				let that = this
				if(this.fileList1.length > 0){
					this.dataForm.userFirstComment.commentImages = this.fileList1.map(item=>item.url).map(item=>this.imageBaseUrl + item.url).join()
				}
				
				addOrderComment(this.dataForm).then(res=>{
					uni.showModal({
						title: '提示',
						content: '提交成功',
						showCancel:false,
						success: function (res) {
							that.navBack()
						}
					});
				})
			},
			
			/**
			 * 回退并刷新
			 */
			navBack(){
				uni.$emit("reloadOrderList")
				uni.navigateBack({})
			},
			
			
		}
	}
</script>
<style>
	page{
		background-color: #fff;
	}
</style>
<style scoped>
	.header{
		/* background-color: #FFB143; */
		background-image: linear-gradient(270deg, #FF9D34 0%, #FFB455 45%, #FFCF7C 100%);
		padding-top: 500rpx;
	}
	
	.icon-size{
		width: 60rpx;
		height: 60rpx;
	}
</style>